# Run summary stats for survey analysis



# Setup ----
library(tidyverse)


# Load survey data as "a"
# SOURCE: please directly contact the authors of You and Kousky (2024)


################################################################################
# Figure 1: Disaster Costs and Funding Choices
################################################################################



# Panel A: Disaster Costs ------------------------------------------------------
cost = a %>%
  data.frame() %>%
  summarise(
            damage_home_Yes      = mean(damage_home_Yes),
            damage_contents_Yes  = mean(damage_contents_Yes),
            damage_car_Yes       = mean(damage_car_Yes),
            service_disrupt_Yes  = mean(service_disrupt_Yes),
            evac_decision_Yes    = mean(evac_decision_Yes),
            costs_additional_Yes = mean(costs_additional_Yes),
            lostincome_dummy     = mean(lostincome_dummy)
            ) 

cost_new = cbind.data.frame(Cost = c("Home damage", "Contents damage", "Car damage", "Service disruptions", "Evacuation costs", "Other costs", "Loss of income"), 
                            Ptg = t(cost))



cost_new$Cost = factor(cost_new$Cost) %>%
  relevel("Service disruptions") %>%
  relevel("Other costs") %>%
  relevel("Home damage") %>%
  relevel("Contents damage") %>%
  relevel("Car damage") %>%
  relevel("Evacuation costs") %>%
  relevel("Loss of income") 


cost_new %>%
  ggplot( aes(x=Cost, y=Ptg*100)) +
  geom_col(width = 0.5) +
  coord_flip() +
  geom_text(aes(label = paste0(format(Ptg*100, digit=2), "%")), hjust = 1.5, color = "white") +
  xlab("") +
  ylab("Percent") +  
  theme_minimal() +
  theme(text = element_text(size = 16)) 




ggsave("/03_Results/01_Figures/Figure1A.jpeg", width = 6, height = 6, dpi=600)






# Panel B: Funding Sources ------------------------------------------------------
Source = a %>%
  data.frame() %>%
  summarise(
    fundingsource_Homeowners_renters_insurance      = mean(fundingsource_Homeowners_renters_insurance),
    fundingsource_Flood_insurance                   = mean(fundingsource_Flood_insurance),
    fundingsource_Family_friends                    = mean(fundingsource_Family_friends),
    fundingsource_Mysavings                         = mean(fundingsource_Mysavings),
    fundingsource_credit_card                       = mean(fundingsource_credit_card),
    fundingsource_FEMA_grant                        = mean(fundingsource_FEMA_grant),
    fundingsource_SBA_loan                          = mean(fundingsource_SBA_loan),
    fundingsource_bank_loan                         = mean(fundingsource_bank_loan),
    fundingsource_charity_nonprofit                 = mean(fundingsource_charity_nonprofit),
    fundingsource_Myemployer                        = mean(fundingsource_Myemployer),
    fundingsource_Local_goverment                   = mean(fundingsource_Local_goverment)
  ) 

Source_new = cbind.data.frame(Source = c("Homeowner/Renter\nInsurance", 
                                     "Flood Insurance", 
                                     "Family/Friends", 
                                     "Savings", 
                                     "Credit Card", 
                                     "FEMA Grant", 
                                     "SBA Loan",
                                     "Private Loan",
                                     "Charity/Nonprofit",
                                     "Employer",
                                     "Local Government"), 
                            Ptg = t(Source))

Source_new$Source = factor(Source_new$Source) %>%
  relevel("Homeowner/Renter\nInsurance") %>%
  relevel("Savings") %>%
  relevel("Family/Friends") %>%
  relevel("Flood Insurance") %>%
  relevel("FEMA Grant") %>%
  relevel("Credit Card") %>%
  relevel("Charity/Nonprofit") %>%
  relevel("Private Loan") %>%
  relevel("Employer") %>%
  relevel("SBA Loan") %>%
  relevel("Local Government")



Source_new %>%
  ggplot( aes(x=Source, y=Ptg*100)) +
  geom_col(width = 0.5) +
  coord_flip() +
  geom_text(aes(label = paste0(format(Ptg*100, digit=0), "%")), 
            hjust = c(1.25, 1.25, 1.25, 1.25, 1.25, 1.25, -0.1, -0.1,-0.1,-0.1,-0.1), 
            color = c("white", "white", "white", "white", "white", "white", "black", "black", "black", "black", "black")) +
  xlab("") +
  ylab("Percent") +  
  theme_minimal() +
  theme(text = element_text(size = 16)) 



ggsave("/03_Results/01_Figures/Figure1B.jpeg", width = 6, height = 6, dpi=600)
